package com.example.mapper;

import com.example.entity.Article;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ArticleMapper {
    @Select("<script>" +
            "SELECT * FROM article " +
            "LIMIT #{offset}, #{limit}" +
            "</script>")
    List<Article> selectArticlesByPage(@Param("offset") int offset, @Param("limit") int limit);

    @Select("SELECT COUNT(*) FROM article")
    int countArticles();

    @Select("<script>" +
            "SELECT * FROM article WHERE title LIKE CONCAT('%', #{titlePart}, '%') " +
            "LIMIT #{offset}, #{limit}" +
            "</script>")
    List<Article> selectArticlesByTitlePart(@Param("offset") int offset, @Param("limit") int limit, @Param("titlePart") String titlePart);

    @Select("SELECT COUNT(*) FROM article WHERE title LIKE CONCAT('%', #{titlePart}, '%')")
    int countArticlesByTitlePart(@Param("titlePart") String titlePart);

    @Select("SELECT * FROM article WHERE id = #{id}")
    Article selectArticleById(@Param("id") Integer id);
}
